package com.foreveross.activiti.service;

import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author:wzq
 * project:activiti_integrate
 * desc: 不做用户做校验
 * date:16-5-13
 */
public class ActivitiService {

    private ProcessEngine processEngine;

    public ActivitiService(ProcessEngine processEngine) {
        this.processEngine = processEngine;
    }

    /**
     * 获取 process definition list
     */
    public List<ProcessDefinition> getProcessDefinitionList() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery()
                .latestVersion().list();

        return processDefinitionList;
    }

    /**
     * 获取 process definition list
     */
    public List<Map<String, String>> getProcessDefinitionListV2() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery()
                .latestVersion().list();

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (int i = 0, j = processDefinitionList.size(); i < j; i++) {
            ProcessDefinition definition = processDefinitionList.get(i);
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", definition.getKey());
            map.put("id", definition.getId());
            list.add(map);
        }

        return list;
    }

//    /**
//     * 获取process definition detail
//     *
//     * @param processDefinitionId
//     */
//    public ProcessDefinition getProcessDefinitionDetail(String processDefinitionId) {
//        RepositoryService repositoryService = processEngine.getRepositoryService();
//        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId)
//                .singleResult();
//        return processDefinition;
//    }


    /**
     * 启动一个 process instance
     *
     * @param uid
     * @param processId
     * @param args
     */
    public String startProcessInstance(String uid, String processId, Map<String, String> args) {
        FormService formService = processEngine.getFormService();
        processEngine.getIdentityService().setAuthenticatedUserId(uid);
        ProcessInstance processInstance = formService.submitStartFormData(processId, args);
        return processInstance.getProcessInstanceId();
    }

    /**
     * 列举 某个用户的任务
     *
     * @param uid
     * @return
     */
    public List<Task> taskAssignee(String uid) {
        TaskService taskService = processEngine.getTaskService();
        List<Task> taskList = taskService.createTaskQuery().taskAssignee(uid).list();
        return taskList;
    }

    /**
     * 获取候选任务
     *
     * @param uid
     * @return
     */
    public List<Task> taskCadidateUser(String uid) {
        TaskService taskService = processEngine.getTaskService();
        List<Task> taskList = taskService.createTaskQuery().taskCandidateUser(uid).list();
        return taskList;
    }

    /**
     * 列举 某个group所有task
     *
     * @param gid
     * @return
     */
    public List<Task> taskCandidateGroup(String gid) {
        TaskService taskService = processEngine.getTaskService();
        List<Task> taskList = taskService.createTaskQuery().taskCandidateGroup(gid).list();
        return taskList;
    }

    public void claimTask(String uid, String taskId) {
        TaskService taskService = processEngine.getTaskService();
        taskService.claim(taskId, uid);
    }


    public Map<String, Object> getProcessVariables(String taskId) {
        RuntimeService runtimeService = processEngine.getRuntimeService();
        TaskService taskService = processEngine.getTaskService();

        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        Map<String, Object> map = runtimeService.getVariables(task.getProcessInstanceId());
        return map;
    }

    /**
     * 完成一项任务
     *
     * @param taskId
     * @param args
     */
    public void completeTask(String taskId, Map<String, String> args) {
        FormService formService = processEngine.getFormService();
        formService.submitTaskFormData(taskId, args);
    }
}
